package com.xyzyzglxt.system.service.impl;

import java.util.List;

import com.xyzyzglxt.common.core.domain.model.LoginUser;
import com.xyzyzglxt.common.core.redis.RedisCache;
import com.xyzyzglxt.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xyzyzglxt.system.mapper.SysEventTypeMapper;
import com.xyzyzglxt.common.core.domain.entity.SysEventType;
import com.xyzyzglxt.system.service.ISysEventTypeService;

/**
 * 活动类型Service业务层处理
 *
 * @author xyzyzglxt
 * @date 2022-01-28
 */
@Service
public class SysEventTypeServiceImpl implements ISysEventTypeService
{
    @Autowired
    private SysEventTypeMapper sysEventTypeMapper;
    @Autowired
    private RedisCache redisCache;


    /**
     * 查找redis缓存
     * @param sysEventType
     * @return
     */
    public List<SysEventType> findCache(SysEventType sysEventType){
        List<SysEventType> sysEventTypeList = redisCache.getCacheList("eventTypeRedis");
        if(sysEventTypeList == null) {
            List<SysEventType> list = sysEventTypeMapper.selectSysEventTypeList(sysEventType);
            redisCache.setCacheList("eventTypeRedis", list);
            redisCache.expire("eventTypeRedis", 43200);
            return (list);
        }
        return (sysEventTypeList);
    }

    /**
     * 更新缓存信息
     */
    public void updateCache(){
        List<SysEventType> list = sysEventTypeMapper.selectSysEventTypeList(new SysEventType());
        redisCache.setCacheList("eventTypeRedis", list);
        redisCache.expire("eventTypeRedis", 43200);
    }



    /**
     * 查询活动类型
     *
     * @param typeId 活动类型主键
     * @return 活动类型
     */
    @Override
    public SysEventType selectSysEventTypeByTypeId(Long typeId)
    {
        return sysEventTypeMapper.selectSysEventTypeByTypeId(typeId);
    }

    /**
     * 查询活动类型列表
     *
     * @param sysEventType 活动类型
     * @return 活动类型
     */
    @Override
    public List<SysEventType> selectSysEventTypeList(SysEventType sysEventType)
    {
        if(sysEventType.getTypeId()==null && sysEventType.getDescription()==null && sysEventType.getTypeName()==null){
            findCache(sysEventType);
        }
        return sysEventTypeMapper.selectSysEventTypeList(sysEventType);
    }

    /**
     * 新增活动类型
     *
     * @param sysEventType 活动类型
     * @return 结果
     */
    @Override
    public int insertSysEventType(SysEventType sysEventType)
    {
        updateCache();
        return sysEventTypeMapper.insertSysEventType(sysEventType);
    }

    /**
     * 修改活动类型
     *
     * @param sysEventType 活动类型
     * @return 结果
     */
    @Override
    public int updateSysEventType(SysEventType sysEventType)
    {
        updateCache();
        return sysEventTypeMapper.updateSysEventType(sysEventType);
    }

    /**
     * 批量删除活动类型
     *
     * @param typeIds 需要删除的活动类型主键
     * @return 结果
     */
    @Override
    public int deleteSysEventTypeByTypeIds(Long[] typeIds)
    {
        updateCache();
        return sysEventTypeMapper.deleteSysEventTypeByTypeIds(typeIds);
    }

    /**
     * 删除活动类型信息
     *
     * @param typeId 活动类型主键
     * @return 结果
     */
    @Override
    public int deleteSysEventTypeByTypeId(Long typeId)
    {
        updateCache();
        return sysEventTypeMapper.deleteSysEventTypeByTypeId(typeId);
    }
}
